/* Copyright (C) 2011  Chris Mueller <ruunhb@googlemail.com>
 *
 * This file is part of the Liquid Programming Language
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "base.h"
#include "tree_generator.h"
#include <glib/goption.h>
#include <glib/gstdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#define RELEASE_OPTION(VAR, FUNCTION) if(VAR != 0) FUNCTION(VAR);

#define ASTVIZ_APP "lq-nodeviz"
#define ASTVIZ_SYSTEM "Unix"
#define ASTVIZ_VERSION "0.1"
#define ASTVIZ_YEAR 2011
#define ASTVIZ_AUTHOR "Christoph Mueller <ruunhb[at]googlemail.com>"

static gchar*   arg_function = 0;
static gboolean arg_version = FALSE;

static GOptionEntry options[] = {
    {"function", 'f', 0, G_OPTION_ARG_STRING, &arg_function, 
        "display only the ast for a specific function", "NAME" },
    {"version", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &arg_version,
        "display the version of this programm", 0 },
    {0}
};


int main(int argc, char** argv)
{
    GError* error = 0;
    GOptionContext* context = g_option_context_new("FILE");

    g_option_context_add_main_entries(context, options, "Options");
    g_option_context_set_summary(context, 
            "lq-nodeviz visualize an abstract syntax tree for a file by using graphviz");

    if(!g_option_context_parse(context, &argc, &argv, &error)) {
        g_printerr("%s\n", error->message);
        return EXIT_FAILURE;
    }

    if(arg_version == TRUE) {
        g_print("%s (%s) %s\n", ASTVIZ_APP, ASTVIZ_SYSTEM, ASTVIZ_VERSION);
        g_print("Copyright (C) %d %s\n", ASTVIZ_YEAR, ASTVIZ_AUTHOR);
        g_print("This is free software; see the source for copying conditions.  There is NO\n");
        g_print("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
        g_print("\n");
        return EXIT_SUCCESS;
    }

    aginit();

    static GVC_t* gvc;
    gvc = gvContext();

    gchar* file = "viz";
    gchar* png = 0;

    unsigned int index = 0;

    while(png == 0) {
        unsigned int size = strlen(file) + 24;
        gchar* image_name = g_new(gchar, size);
        g_snprintf(image_name, size, "%s.%d.png", file, index);

        if(g_access(image_name, F_OK) != 0)
            png = image_name;
        else
            g_free(image_name);

        index++;
    }

    TreeGenerator* t = lq_nodeviz_treegen_new();

    lq_nodeviz_treegen_read(t, stdin);

    Agraph_t* g = lq_nodeviz_treegen_graph(t, 0);
    
    gvLayout(gvc, g, "dot");
    gvRenderFilename(gvc, g, "png", png);

    gvFreeLayout(gvc, g);
    gvFreeContext(gvc);
    agclose(g);
    lq_nodeviz_treegen_free(t);
    g_free(png);
    
    RELEASE_OPTION(arg_function, g_free);

    printf("\n");

    return EXIT_SUCCESS;
}
